//
//  SuitUIV.m
//  mbstore
//
//  Created by huangyi on 14-3-17.
//  Copyright (c) 2014年 huangyi. All rights reserved.
//

#import "SuitUIV.h"
#import "ProductListUIP.h"
#import "UIImageView+WebCache.h"
//#import "UIImageView+AFNetworking.h"
@implementation SuitUIV

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

- (IBAction)PressUp:(id)sender {
    //    [self.scrollSuit setContentOffset:CGPointMake(0, self.scrollSuit.contentOffset.y-self.scrollSuit.contentSize.width-24) animated:YES];
}

- (IBAction)PressDown:(id)sender {
    //     [self.scrollSuit setContentOffset:CGPointMake(0, self.scrollSuit.contentOffset.y+self.scrollSuit.contentSize.width+24) animated:YES];
}

-(IBAction)onProductClick:(UIButton *)sender{
    
    if (_delegate.tagType==1) {
        if (productDetail.hidden) {
            productDetail.hidden=NO;
            [self ShowView:productDetail];
            [sender setTitle:@"-信息" forState:UIControlStateNormal];
        }
        else{
            [self HideView:productDetail];
            [sender setTitle:@"+信息" forState:UIControlStateNormal];
        }
    }
    else{
        if (proInfoVie.hidden) {
            proInfoVie.hidden=NO;
            [self ShowView:proInfoVie];
            [sender setTitle:@"-信息" forState:UIControlStateNormal];
        }
        else{
            [self HideView:proInfoVie];
            [sender setTitle:@"+信息" forState:UIControlStateNormal];
        }
    }
    
    //    ItemUI *item=self.delegate;
    //    NSDictionary *suit=[item suitDict];
    //    NSString *itemId=suit[@"item_id"];
    //    #warning 调用数据库
    //    NSArray *pdts=[AppUI getProductBySuit:itemId];
    //    NSMutableArray *items=[NSMutableArray array];
    //    for (NSString *pdt in pdts) {
    //        #warning 调用数据库
    //        NSArray *skus=[AppUI getProductSku:pdt];
    //        NSDictionary *sku=[skus firstObject];
    //        if (sku) {
    //            [items addObject:sku[@"sku_id"]];
    //        }
    //    }
    //    ProductListUIP *list=[[ProductListUIP alloc] initWithStyle:UITableViewStylePlain];
    //    list.delegate=self;
    //    [list setItems:items];
    //    [list showFromView:sender];
}
-(void)ShowView:(UIView *)view{
    [UIView animateWithDuration:0.3 animations:^{
        view.alpha=1.0;
        
    } completion:^(BOOL finished) {
        
    }];
}
-(void)HideView:(UIView *)view{
    [UIView animateWithDuration:0.3 animations:^{
        view.alpha=0.0;
    } completion:^(BOOL finished) {
        view.hidden=YES;
    }];
    
}
-(IBAction)onSwipeLeft:(id)sender{
    //    ItemUI *item=self.delegate;
    //    item.suitIdx++;
}
-(IBAction)onSwipeRight:(id)sender{
    //    ItemUI *item=self.delegate;
    //    item.suitIdx--;
}

-(void)showProductItem:(NSString *)itemId{
    if (itemId) {
        ItemUI *item=self.delegate;
        item.productId=itemId;
    }
}

-(void)setDelegate:(ItemUI *)delegate{
    
    _delegate=delegate;
    //    [self setSuitView];
    [self.productButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [self.productButton setTintColor:[UIColor darkGrayColor]];
    [self.productButton.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [self.productButton.layer setBorderWidth:1];
    //    [self.vieContentMoney.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    //    [self.vieContentMoney.layer setBorderWidth:1];
    self.labStatrPrice.strikeThroughEnabled=YES;
    
    if (_delegate.tagType==1) {
        productDetail=[[ProductDetail alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.productButton.frame)-260, CGRectGetMaxY(self.productButton.frame)+4, 260, 600)];
        productDetail.alpha=0;
        productDetail.hidden=YES;
        productDetail.isSuit=YES;
        //            productDetail.dicDetail=suit;
        [self addSubview:productDetail];
        
    }
    else{
        proInfoVie=[[ProductInfo alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.productButton.frame)-260, CGRectGetMaxY(self.productButton.frame)+4, 260, 600)];
        proInfoVie.alpha=0;
        //            proInfoVie.items=suit;
        //        proInfoVie.itemDelegate=_delegate;
        proInfoVie.hidden=YES;
        [self addSubview:proInfoVie];
    }

    
    
    responseCtr=[[MBStoreSrv alloc]init];
    //    [self GetData];
    ItemUI *item=_delegate;
    
    [item suitDict:^(NSDictionary *dicData) {
        suit= [NSMutableDictionary dictionaryWithDictionary:dicData];
        if (productDetail) productDetail.dicDetail=suit;
        else proInfoVie.items=suit;
        [self setSuitView];
    }];

    self.btnUp.enabled=NO;
}

-(void)setSuitView{
    NSString *photos=suit[@"mainPhoto"];
    //    NSArray *items=[photos componentsSeparatedByString:@","];
    NSArray *items=[NSArray arrayWithObjects:photos, nil];
    self.photos=items;
    [self.scrollView removeAllSubviews];
    [self.scrollSuit removeAllSubviews];
    for (int i=0; i<items.count; i++) {
        //        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, 740)];
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectZero];
        imageView.tag=i;
        imageView.frame=CGRectMake(0, 10, self.scrollView.frame.size.width, self.scrollView.frame.size.height-65);
        imageView.contentMode=UIViewContentModeScaleAspectFit;
        [self.scrollView addSubview:imageView];
        //        imageView.frame=CGRectOffset(self.bounds, 0, CGRectGetHeight(self.bounds)*i);
        NSArray *arrSuit=suit[@"suits"];
        if (suit[@"goods"]) {
            arrSuit=suit[@"goods"];
        }
        UIImageView *collocation;
        for (int j=0; j<arrSuit.count; j++) {
            collocation=[[UIImageView alloc]initWithFrame:CGRectMake(0, j*(CGRectGetWidth(self.scrollSuit.bounds)+24), CGRectGetWidth(self.scrollSuit.bounds), CGRectGetWidth(self.scrollSuit.bounds))];
            collocation.tag=3012+j;
            NSString *imgUrl=[[arrSuit objectAtIndex:j] objectForKey:@"mainPhoto"];
            [imgUrl stringByDeletingPathExtension];
           imgUrl= [NSString stringWithFormat:@"%@--w_144_h_144.jpg",imgUrl];
            [collocation setImageWithURL:[NSURL URLWithString:[[arrSuit objectAtIndex:j] objectForKey:@"mainPhoto"]]];
            [collocation.layer setBorderWidth:1];
            collocation.userInteractionEnabled=YES;
            [collocation.layer setBorderColor:[UIColor lightGrayColor].CGColor];
            collocation.contentMode=UIViewContentModeScaleAspectFit;
            [self.scrollSuit addSubview:collocation];
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSuitClick:)];
            [collocation addGestureRecognizer:tap];
            
        }
        self.scrollSuit.contentSize=CGSizeMake(CGRectGetWidth(self.scrollSuit.frame), CGRectGetMaxY(collocation.frame));
        if (arrSuit.count<4) {
            if (arrSuit.count==0) {
                self.vieContentPro.hidden=YES;
            }
            else{
            self.vieContentPro.frame=CGRectMake(self.vieContentPro.frame.origin.x, self.vieContentPro.frame.origin.y+(4.0-arrSuit.count)/2.0*self.vieContentPro.frame.size.width, self.frame.size.width, CGRectGetMaxY(collocation.frame)+108);
            self.btnDown.enabled=NO;
            self.btnUp.enabled=NO;
            }
        }
       
        
    }
    [self.scrollView flashScrollIndicators];
    [self scrollViewDidScroll:self.scrollView];
}
//点击左边图片
-(void)onSuitClick:(UITapGestureRecognizer *)sender{
    UIView *itemView=sender.view;
    int idx=itemView.tag-3012;
    
   
    ItemUI *itme=self.delegate;
   
    NSArray *arrSuit;
    if (suit[@"goods"]) {
        arrSuit=suit[@"goods"];
        itme.tagType=1;
         itme.productId=[arrSuit objectAtIndex:idx][@"colorSn"];
    }
    else{
        arrSuit=suit[@"suits"];
         itme.tagType=2;
        NSString *str=[arrSuit objectAtIndex:idx][@"itemId"];
        NSRange rang=[str rangeOfString:@"-J"];
        str=[str substringToIndex:rang.location];
        str=[str stringByReplacingOccurrencesOfString:@"-" withString:@""];
         itme.productId=str;
        
    }
   
//    NSLog(@"%@",[arrSuit objectAtIndexedSubscript:idx]);
    
}
-(void)setPhotos:(NSArray *)photos{
    _photos=photos;
    self.pageIndicator.numberOfPages=photos.count;
    self.pageIndicator.currentPage=0;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    //
    //    NSArray *subviews=self.scrollView.subviews;
    //    for (int i=0; i<subviews.count; i++) {
    //        UIView *view=[subviews objectAtIndex:i];
    ////        view.frame=CGRectOffset(self.bounds, 0, CGRectGetHeight(self.bounds)*i);
    //         view.frame=CGRectMake(0, 10, self.scrollView.frame.size.width, self.scrollView.frame.size.height-65);
    //    }
    //    self.scrollView.contentSize=CGSizeMake(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)*self.pageIndicator.numberOfPages);
    //    self.scrollView.contentOffset=CGPointMake(0, CGRectGetHeight(self.bounds)*self.pageIndicator.currentPage);
    //    NSLog(@"%f",self.scrollView.contentSize.width);
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([scrollView isEqual:self.scrollView]){
        int offset=scrollView.contentOffset.y;
        int page=offset/CGRectGetHeight(self.bounds);
        self.pageIndicator.currentPage=page;
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if ([scrollView isEqual:self.scrollView]) {
        scrollView.showsHorizontalScrollIndicator=NO;
        scrollView.showsVerticalScrollIndicator=NO;
        CGRect vis=CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, CGRectGetWidth(scrollView.bounds), CGRectGetHeight(scrollView.bounds));
        NSLog(@"%@",scrollView.subviews);
        for (id view in scrollView.subviews) {
            if ([view isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView=view;
                if (CGRectIntersectsRect(vis, imageView.frame)) {
                    int idx=imageView.tag;
                    NSString *imageURL;
                    //                if (idx==0) {
                    imageURL=[self.photos objectAtIndex:0];
                    //                }
                    imageURL=[AppUI getImageURL:imageURL];
                    [imageView setImageURL:imageURL];
                }else{
                    imageView.image=nil;
                }
            }
        }
        scrollView.showsHorizontalScrollIndicator=YES;
        scrollView.showsVerticalScrollIndicator=YES;
        self.pageIndicator.hidden=YES;
    }
    else{
        self.btnUp.enabled=YES;
        self.btnDown.enabled=YES;
        if (scrollView.contentOffset.y>=scrollView.contentSize.height-scrollView.frame.size.height-24) {
            self.btnDown.enabled=NO;
            NSLog(@"下");
        }
        else if(scrollView.contentOffset.y<=0){
            self.btnUp.enabled=NO;
            NSLog(@"上");
        }
    }
    
}

@end
